package middleware

import (
	"context"
	"net/http"
	"web/global"
	"web/utils"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

//路由过滤器
func Filter(ctx *gin.Context) {
	//登录校验
	session := sessions.Default(ctx)
	userName := session.Get("userName")
	resp := make(map[string]interface{})
	if userName == nil {
		resp["errno"] = utils.RECODE_SESSIONERR
		resp["errmsg"] = utils.RecodeText(utils.RECODE_SESSIONERR)
		ctx.JSON(http.StatusOK, resp)
		ctx.Abort()
		return
	}

	//计算这个业务耗时

	//执行函数
	ctx.Next()
}

func Logger(ctx *gin.Context) {
	global.GlobalLog.Error(context.Background(), ctx.Request.RemoteAddr+"--"+ctx.Request.Method+"--URL:", ctx.Request.URL)
	// fmt.Println(ctx.Request.URL, ctx.Request.Method, ctx.Request.RemoteAddr)
}
